set more 1
set scheme plotplain
graph set window fontface "Times New Roman"
use ..\data\paper_data.dta, clear

global style_lines_base "lwidth(1.2) lpattern(solid) msym(Sh) msize(2.5) col(g)"
global style_lines_dec "lwidth(1.2) lpattern(longdash) msym(Th) msize(3.5) col(gs10)"
global style_lines_str "lwidth(1.2) lpattern(dash_dot) msym(Oh) msize(3) col(gs5)"
global color_base " white "
global color_dec " gs11 "
global color_str " gs6 "

preserve

*Figure C.1: Global geoengineering in economies of two
*=====================================================

collapse g if part<3 & treatment_id<3, by(treatment_id economy_type economy_id part period)
bysort treatment_id economy_type: egen g_avg=mean(g)
replace g_avg=round(g_avg,0.1)
replace g=15 if g>15
gen ne=3 if treatment_id==0 & g==10
replace ne=3 if treatment_id==0 & g==6 & economy_type==1
replace ne=3 if treatment_id>0 & g==2 & part==2 & economy_type<3
replace ne=3 if treatment_id>0 & g==6 & part==2 & economy_type==3
gen high=77
gen low=0

twoway(hist g if treatment_id==0, percent disc fcolor($color_base)) ///
 (hist g if treatment_id==1, percent disc bcolor($color_dec)) ///
 (hist g if treatment_id==2, percent disc bcolor($color_str)) ///
 (rcap high low g_avg if treatment_id==0, color(gs0) msize(0)) /// 
 (rcap high low g_avg if treatment_id==1, color(gs0) msize(0)) /// 
 (rcap high low g_avg if treatment_id==2, color(gs0) msize(0)) ///
 (scatter high g_avg if treatment_id==0, mlabel(g_avg) mlabcolor(gs0) mlabsize(vsmall) msize(0)) /// 
 (scatter high g_avg if treatment_id==1, mlabel(g_avg) mlabcolor(gs0) mlabsize(vsmall) msize(0)) /// 
 (scatter high g_avg if treatment_id==2, mlabel(g_avg) mlabcolor(gs0) mlabsize(vsmall) msize(0)) ///
 (scatter ne g if treatment_id==0, msym(S) mcolor(gs0) msize(1.2)) /// 
 (scatter ne g if treatment_id==1, msym(T) mcolor(gs0) msize(1.2)) /// 
 (scatter ne g if treatment_id==2, msym(O) mcolor(gs0) msize(1.2)), /// 
 by(treatment_id economy_type, r(3) note("") legend(off)) plotr(m(sides)) subtitle(, size() fcolor(white) nobox) ///
 xlab(0(1)14 15 "    {&ge}15", nogrid labsize(vsmall)) ylab(0(10)80, labsize(vsmall)) ///
xtitle("Global geoengineering", size(large)) ytitle("%", size(large))
graph export ..\figs\g_hist_n2.pdf, replace	

restore
preserve

* Figure C.2: Evolution of global geoengineering
*===============================================

collapse g if treatment_id<3, by(treatment_id part period)

* (a) N=2
twoway(connected g period if treatment_id==0 & part<3, $style_lines_base) ///
(connected g period if treatment_id==1 & part<3, $style_lines_dec) ///
(connected g period if treatment_id==2 & part<3, $style_lines_str), ///
xtitle("Period", size(large))  plotr(m(sides)) ///
ytitle("Global geoengeering", size(large)) ///
xlabel(1(1)10, labsize(medium) nogrid) ///
ylabel(0(2)10, labsize(medium)) ///
text(7.6 9.8 "Baseline", color(black) size(medium)) ///
text(9.5 9.3 "Decentralized", color($color_amount) size(medium)) ///
text(5 9 "Structured", color($color_stru) size(medium)) ///
xline(5) ///
graphregion(color(white)) legend(off)
graph export ..\figs\g_rounds_n2.pdf, replace

* (b) N=6
twoway(connected g period if treatment_id==0 & part==3, $style_lines_base) ///
(connected g period if treatment_id==1 & part==3, $style_lines_dec) ///
(connected g period if treatment_id==2 & part==3, $style_lines_str), ///
xtitle("Period", size(large))  plotr(m(sides)) ///
ytitle("Global geoengeering", size(large)) ///
xlabel(11(1)25, labsize(medium) nogrid) ///
ylabel(0(2)14, labsize(medium)) ///
text(12.8 19 "Baseline", color(black) size(medium)) ///
text(13 16 "Decentralized", color($color_amount) size(medium)) ///
text(6.5 16 "Structured", color($color_stru) size(medium)) ///
graphregion(color(white)) legend(off) 
graph export ..\figs\g_rounds_n6.pdf, replace

restore
preserve

* Figure C.3: Evolution of global geoengineering in Decentralized with N = 2–Robustness
*======================================================================================

collapse g if treatment_id==3, by(economy_type period)

label define economy_type /// 
  1 "{bf: (a)} Economy ({it:L}, {it:M})" /// 
  2 "{bf: (b)} Economy ({it:L}, {it:H})" /// 
  3 "{bf: (c)} Economy ({it:M}, {it:H})", replace 
twoway(line g period, lwidth(0.6) col(black)), by(economy_type, r(1) note("")) subtitle(, size(vlarge) fcolor(white) nobox) ///
xtitle("Period", size(medium))  plotr(m(sides)) ///
ytitle("Global geoengeering", size(medium)) ///
xlab(1 2(2)25, labsize(small) nogrid) ylab(0(2)13, labsize(small)) xline(5) xsize(4.5) ysize(2)
graph export ..\figs\g_rounds_decn2only.pdf, replace

restore
preserve

* Figure E.4: Effort composition
*===============================

gen gIP2=g_team/2 if gstar==2 // divided by 2 just two avoid the problem in the collapse
gen gIP6=g_team/2 if gstar==6
gen gIP10=g_team/2 if gstar==10
collapse (mean)g (sum)gIP* if part>1 & treatment_id<3, by(treatment_id economy_id economy_type part period)
collapse (mean)g gIP*, by(treatment_id economy_type part)
set obs `=_N+2'
gen xaxis=1
replace gIP6=gIP6+gIP10
replace gIP2=gIP2+gIP6
replace treatment_id=3 if _n==13
replace treatment_id=4 if _n==14
replace gIP10=6 if treatment_id==3
replace gIP10=10 if treatment_id==4
replace part=3 if part==.
label define treatment_id /// 
  0 "Baseline" /// 
  1 "Decentralized" ///
  2 "Structured" ///
  3 "Social optimum" ///
  4 "Free-driving", replace 
label value treatment_id treatment_id

* (a) N=6
twoway(bar gIP2 xaxis if part==3, fcolor(gs0) lcolor(black)) ///
(bar gIP6 xaxis if part==3, fcolor(gs8) lcolor(black)) ///
(bar gIP10 xaxis if part==3, fcolor(gs15) lcolor(black)), ///
 by(treatment_id, r(1) note("") legend(pos(3))) plotr(m(sides)) subtitle(, size(medium) fcolor(white) nobox) ///
 xtitle("") ytitle("Average team effort", size(large)) ///
 xla(, labcolor(bg) tlength(0) nogrid) yla(0(2)11, labsize(medium)) ///
 legend(col(1) order(1 "{it:L}" 2 "{it:M}" 3 "{it:H}")) 
 graph export ..\figs\effort_composition_n6.pdf, replace

* (b) N=2
drop if part==3
gen gL=gIP2
replace gL=gIP6 if economy_type==3
gen gH=gIP10
replace gH=gIP6 if economy_type==1 
replace gL=gH+gL
 twoway(bar gIP2 xaxis if part==2, fcolor(gs0) lcolor(black)) ///
(bar gIP6 xaxis if part==2, fcolor(gs8) lcolor(black)) ///
(bar gIP10 xaxis if part==2, fcolor(gs15) lcolor(black)), ///
 by(treatment_id economy_type, r(3) note("") legend(pos(3))) plotr(m(sides)) subtitle(, size(medsmall) fcolor(white) nobox) ///
 xtitle("") ytitle("Average team effort", size(large)) ///
 xla(, labcolor(bg) tlength(0) nogrid) yla(0(2)11, labsize(medium)) ///
 legend(col(1) order(1 "{it:L}" 2 "{it:M}" 3 "{it:H}")) 
 graph export ..\figs\effort_composition_n2.pdf, replace

restore
preserve 
 
* Figure C.5: Evolution of mutually beneficial offers
*====================================================

keep if part==2 & (treatment_id==1 | treatment_id==2)
collapse (mean)gstar amount_team request_team sign_team gstar_opponent if high_gstar==0 & offer_made==1, by(treatment_id economy_id team_id period economy_type)

replace request_team=request_team-1 if sign_team==-1
replace request_team=gstar_opponent if sign_team==1 & request_team<gstar_opponent
gen pred_amount=34 if request_team==2 & economy_type==1
replace pred_amount=70 if request_team==2 & economy_type==2
replace pred_amount=34 if request_team==6 & economy_type==3

gen profitable_sender=-amount_team-10*abs(gstar-request_team)>=-10*abs(gstar-gstar_opponent)
gen profitable_recipient=-4*request_team-10*abs(gstar_opponent-request_team)+amount_team>-4*gstar_opponent
gen profitable_both=(profitable_recipient+profitable_sender==2)
collapse profitable_both, by(treatment_id period)
twoway(line profitable_both period if treatment_id==1, lwidth(0.6) color(black)) ///
(line profitable_both period if treatment_id==2, $style_lines_str lwidth(0.6) color(gs8)), xlab(, nogrid) ytitle("Share") xtitle("Period") legend(pos(6) r(1) label(1 "Decentralized") label(2 "Structured") size(medium))
graph export ..\figs\profitableoffers_rounds.pdf, replace

restore
preserve

* Figure E.1: Offers' dynamics over periods–Bug in Structured
*============================================================

keep if part==3
drop if treatment_id==3
gen treatment_id2=treatment_id
replace treatment_id2=3 if date=="180618_0927" | (date=="180618_1428" & economy_id==61) | date=="180620_0947"

collapse high_gstar gstar target_team offer_made offer_taken if treatment_id2>1, by(treatment_id treatment_id2 economy_id team_id period)

gen offer_made_ip10=offer_made if treatment_id==2
replace offer_made_ip10=offer_made if treatment_id==1 & high_gstar==0 & target_team>=5 & target_team<.
gen offer_taken_ip10=offer_taken if treatment_id==2
replace offer_taken_ip10=offer_taken if treatment_id==1 & target_team>=5 & target_team<.
bysort economy_id period: egen n_offers_made_ip10=sum(offer_made_ip10)
bysort economy_id period: egen n_offers_fulfilled_ip10=sum(offer_taken_ip10)

collapse (max)offer_made offer_taken n_*, by(treatment_id2 economy_id period)
replace offer_taken=0 if offer_taken==.
replace n_offers_fulfilled_ip10=0 if n_offers_fulfilled_ip10==.
collapse offer_made offer_taken n_offers_*, by(treatment_id2 period)
label define treatment_id2 /// 
  2 "{bf:(a)} Structured without bug" ///
  3 "{bf:(b)} Structured with bug", replace
label value treatment_id2 treatment_id2
lab var period "Period"

* (a) Offer making and taking rate
twoway (line offer_made period, lpattern(dash) color(black) lwidth(0.6))(line offer_taken period, lpattern(solid) color(gs8) lwidth(0.6)), ///
by(treatment_id2, note("")) subtitle(, size(medium) fcolor(white) nobox) ///
legend(order(1 "Offers" 2 "Side-payments") r(1) size(medium)) ///
ytitle("Rate") xlab(, nogrid)
graph export ..\figs\offer_round_a_robcheck_structured.pdf, replace 

* (b) Average number of offers made to and taken by high-ideal point team
twoway (line n_offers_made_ip10 period, lpattern(dash) color(black) lwidth(0.6))(line n_offers_fulfilled_ip10 period, lpattern(solid) color(gs8) lwidth(0.6)), ///
by(treatment_id2, note("")) subtitle(, size(medium) fcolor(white) nobox) ytitle("Number") xlab(, nogrid) ///
legend(order(1 "Offers" 2 "Side-payments") r(1) size(medium)) ///
ytitle("Number")
graph export ..\figs\offer_round_b_robcheck_structured.pdf, replace

restore
preserve

* Figure D.1: Within-team disagreements
*======================================

gen disagreement_effort_any=(g_own_1!=g_partner_1)
gen disagreement_effort_unsolved=(disagreement_effort_any==1 & g_own_2!=g_partner_2)
gen disagreement_offer=(amount_own_1!=amount_partner_1) | (request_own_1!=request_partner_1) | (sign_own_1!=sign_partner_1) | (target_own_1!=target_partner_1)
gen disagreement_offer_unsolved=((amount_own_1!=amount_partner_1) & (amount_own_2!=amount_partner_2)) | ((request_own_1!=request_partner_1) & (request_own_2!=request_partner_2)) | ((sign_own_1!=sign_partner_1) & (sign_own_2!=sign_partner_2)) | ((target_own_1!=target_partner_1) & (target_own_2!=target_partner_2))
bysort economy_id period: egen disagreement_effort_unsolved_max=max(disagreement_effort_unsolved)
bysort economy_id period: egen disagreement_offer_unsolved_max=max(disagreement_offer_unsolved)
collapse disagreement* if treatment_id<3, by(treatment_id economy_id part period)
collapse disagreement*, by(treatment_id period)

* (a) Effort disagreement
twoway(line disagreement_effort_unsolved_max period if treatment_id==0, $style_lines_base lwidth(0.6)) ///
(line disagreement_effort_unsolved_max period if treatment_id==1, $style_lines_dec lwidth(0.6)) ///
(line disagreement_effort_unsolved_max period if treatment_id==2, $style_lines_str lwidth(0.6)), ///
graphregion(color(white)) plotr(m(none)) /// 
title(, color(gs0)) ///
xlabel(1(1)25, labsize(small) nogrid) xtitle("Period") ylabel(0(0.1)0.5, labsize(small) angle(0) nogrid) ytitle("Share of economies with" "at least one disagreement in effort decisions") ///
xline(5, lcolor(black) lwidth(0.1)) xline(10, lcolor(black) lwidth(0.1)) ///
legend(row(3) ring(0) pos(11) order( ///
			3 "Structured" ///
			2 "Decentralized" ///
			1 "Baseline") ///
region(col(none)))
graph export ..\figs\disagreement_effort.pdf, replace

* (b) Offer disagreement
twoway(line disagreement_offer_unsolved_max period if treatment_id==1 & period>5, $style_lines_dec lwidth(0.6)) ///
(line disagreement_offer_unsolved_max period if treatment_id==2 & period>5, $style_lines_str lwidth(0.6)), ///
graphregion(color(white)) plotr(m(none)) /// 
title(, color(gs0)) ///
xlabel(6(1)25, labsize(small) nogrid) xtitle("Period") ylabel(0(0.1)0.5, labsize(small) angle(0) nogrid) ytitle("Share of economies with" "at least one disagreement in offer decisions") ///
xline(10, lcolor(black) lwidth(0.1)) ///
legend(row(3) ring(0) pos(11) order( ///
			2 "Structured" ///
			1 "Decentralized") ///
region(col(none)))
graph export ..\figs\disagreement_offer.pdf, replace

restore

* Figure G.1: Distribution of mistakes by treatment
*==================================================

keep if period==1
drop if treatment_id==3
bysort team_id (subject_id period): gen rank=_n
gen mistakes=mistakes_pt1+mistakes_pt2+mistakes_pt3

label define treatment_id /// 
  0 "{bf: (a)} Baseline" ///
  1 "{bf: (b)} Decentralized" ///
  2 "{bf: (c)} Structured", replace 
label value treatment_id treatment_id
hist mistakes if rank==1, by(treatment_id, c(3) note("") graphregion(color(white))) ///
subtitle(, size(medlarge) fcolor(white) nobox) ///
ylab(0(20)100, labsize(medsmall) ) xlabel(0(1)5, labsize(medsmall) nogrid) xtitle("Number of mistakes") ytitle("Percentage of teams") percent disc 
graph export ..\figs\mistakes_team.pdf, replace


* Figure H.2: Classification of the chat content by treatment, part, and stage (layout maually adjusted using chat_content_edit.gph)
*=============================================================================

use ..\data\coders_output.dta, clear

global cat_offer "confused_us confused_them coordination difficult prosocial antisocial overconfident disagree giveuppromises"
foreach v of varlist $cat_offer{
	replace `v'=`v'/rowtot
}

bysort team_id (period): gen economy_id_n6=economy_id[_N]
grstyle init 
grstyle set color #e41a1c #377eb8 #4daf4a #984ea3 #ff7f00 #ffff33 #999999 #f781bf #a65628
collapse $cat_offer, by(treatment_id part effort_stage economy_id_n6)

graph pie $cat_offer if part>1, by(treatment_id effort_stage part, col(2) note("", size(zero) margin(zero)) graphregion(margin(zero))) plotregion(margin(zero)) subtitle("", size(*0.1)) xsize(3) ysize(4) ///
legend(c(3) size(*1.1) symxsize(1) symysize(1) rowgap(0.1) region(margin(zero) lstyle(none)) ///
order(1 "Confused us" ///
	  2 "Confused them" ///
	  3 "Coordination" ///
	  4 "Difficult" ///
	  5 "Prosocial" ///
	  6 "Antisocial" ///
	  7 "Smooth" ///
	  8 "Disagree" ///
	  9 "Give up on offers" ///
	  ))
graph export ..\figs\chat_content.pdf, replace